package com.wtf.dsp.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wtf.dsp.admin.entity.WtfDspUser;
import com.wtf.dsp.admin.enums.CommonErrorEnum;
import com.wtf.dsp.admin.exception.WtfDspException;
import com.wtf.dsp.admin.mapper.WtfDspUserMapper;
import com.wtf.dsp.admin.model.LoginUser;
import com.wtf.dsp.admin.model.LoginUserContext;
import com.wtf.dsp.admin.model.dto.UserAddDTO;
import com.wtf.dsp.admin.model.dto.UserDelDTO;
import com.wtf.dsp.admin.model.dto.UserPageDTO;
import com.wtf.dsp.admin.model.dto.UserUpdateDTO;
import com.wtf.dsp.admin.model.vo.UserPageVO;
import com.wtf.dsp.admin.service.UserService;
import com.wtf.dsp.core.util.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: UserServiceImpl
 * Description:
 * date: 2023/8/3 18:29
 *
 * @author linhuanyou
 * @version 1.0
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    private static final String ADMIN = "wtf";

    @Autowired
    private WtfDspUserMapper wtfDspUserMapper;

    @Override
    public PageUtils page(UserPageDTO userPageDTO) {
        Page<?> page = PageHelper.startPage(userPageDTO.getPage(), userPageDTO.getLimit());
        LambdaQueryWrapper<WtfDspUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasLength(userPageDTO.getUsername())){
            lambdaQueryWrapper.like(WtfDspUser::getUsername,userPageDTO.getUsername());
        }
        List<UserPageVO> list = wtfDspUserMapper.selectList(lambdaQueryWrapper).stream().map(e->{
            UserPageVO userPageVO = new UserPageVO();
            userPageVO.setUsername(e.getUsername());
            return userPageVO;
        }).collect(Collectors.toList());
        return new PageUtils(list, (int) page.getTotal(), userPageDTO.getLimit(),userPageDTO.getPage());
    }

    @Override
    public int add(UserAddDTO userAddDTO) {
        if(wtfDspUserMapper.selectById(userAddDTO.getUsername()) != null){
            throw new WtfDspException(CommonErrorEnum.USER_EXIST);
        }
        LoginUser loginUser = LoginUserContext.loginUser();
        String username = loginUser.getUsername();
        if(!ADMIN.equals(username)){
            throw new WtfDspException(CommonErrorEnum.ADMIN_PERMISSION_DENIED);
        }
        WtfDspUser wtfDspUser = new WtfDspUser();
        wtfDspUser.setUsername(userAddDTO.getUsername());
        wtfDspUser.setPassword(DigestUtils.md5DigestAsHex(userAddDTO.getPassword().getBytes()));
        return wtfDspUserMapper.insert(wtfDspUser);
    }

    @Override
    public int remove(UserDelDTO userDelDTO) {
        if(userDelDTO.getUsernames().contains(ADMIN)){
            throw new WtfDspException(CommonErrorEnum.ADMIN_USERNAME);
        }
        LoginUser loginUser = LoginUserContext.loginUser();
        String username = loginUser.getUsername();
        if(!ADMIN.equals(username)){
            throw new WtfDspException(CommonErrorEnum.ADMIN_PERMISSION_DENIED);
        }
        return wtfDspUserMapper.deleteBatchIds(userDelDTO.getUsernames());
    }

    @Override
    public int update(UserUpdateDTO userUpdateDTO) {
        WtfDspUser wtfDspUser = wtfDspUserMapper.selectById(userUpdateDTO.getUsername());
        if(wtfDspUser == null){
            throw new WtfDspException(CommonErrorEnum.USER_NOT_EXIST);
        }
        String password = wtfDspUser.getPassword();
        if(!DigestUtils.md5DigestAsHex(userUpdateDTO.getOldPassword().getBytes()).equals(password)){
            throw new WtfDspException(CommonErrorEnum.PASSWORD_ERROR);
        }
        if(userUpdateDTO.getOldPassword().equals(userUpdateDTO.getNewPassword())){
            throw new WtfDspException(CommonErrorEnum.OLD_AND_NEW_PASSWORD_ERROR);
        }
        LoginUser loginUser = LoginUserContext.loginUser();
        String username = loginUser.getUsername();
        if(!username.equals(userUpdateDTO.getUsername()) && !ADMIN.equals(username)){
            throw new WtfDspException(CommonErrorEnum.LOGIN_PERMISSION_DENIED);
        }
        WtfDspUser newWtfDspUser = new WtfDspUser();
        newWtfDspUser.setUsername(wtfDspUser.getUsername());
        newWtfDspUser.setPassword(DigestUtils.md5DigestAsHex(userUpdateDTO.getNewPassword().getBytes()));
        return wtfDspUserMapper.updateById(newWtfDspUser);
    }
}
